package com.qdxwx.web.controller.user;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKRequest;
import com.qdxwx.data.service.favorite.FavoriteService;
import com.qdxwx.data.service.teacher.TeacherService;
import com.qdxwx.models.enums.ModuleType;
import com.qdxwx.models.favorite.Favorite;
import com.qdxwx.models.teacher.Teacher;
import com.qdxwx.web.controller.BaseController;
import com.qdxwx.web.utils.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 个人中心  Controller
 * @author developer
 */
@Controller
@RequestMapping("/user")
public class UserFavoriteController extends BaseController {

	@Autowired
	private FavoriteService favoriteService;
	@Autowired
	private TeacherService teacherService;

	/**
	 * 我的收藏列表
	 */
	@RequestMapping("/favorite.html")
	public String myFavorites(HttpServletRequest request, ModelMap mmap) {
		long userId = LoginUser.getUserId(request);
		if (userId < 1) {
			return "redirect:/auth/login.html";
		}
		int typeId = TKConvert.toInteger(request.getParameter("typeId"), 2);
		int currentPage = TKConvert.toInteger(request.getParameter("page"), 1);
		int limit = TKConvert.toInteger(request.getParameter("limit"), 5);
		PageRequest page = new PageRequest(currentPage, limit);
		List<Favorite> favoriteList = favoriteService.getList(userId, typeId, page);
		if (typeId == ModuleType.COURSE.ordinal()) {
			for (Favorite favorite : favoriteList) {
				List<Teacher> teachers = teacherService.getListByCourseId(favorite.getInfoId());
				if (teachers != null) {
					favorite.setTeacherList(teachers);
				}
			}
		}
		int total = favoriteService.count(userId, typeId);
		mmap.put("typeId", typeId);
		mmap.put("courseCount", favoriteService.count(userId, 2));
		mmap.put("bookCount", favoriteService.count(userId, 3));
		mmap.put("pictureCount", favoriteService.count(userId, 4));
		mmap.put("documenrCount", favoriteService.count(userId, 5));
		mmap.put("totalPages", page.getTotalPages(total));
		mmap.put("favoriteList", favoriteList);
		mmap.put("page", page);
		mmap.put("favoritesListUri", TKRequest.getRequestUriWithParams(request));
		mmap.put("user", LoginUser.getUser(request));
		if (TKRequest.isMobileBrowse(request)) {
			return getViewPathH5("user/uc-favorite");
		}
		return getViewPath("ucenter/uc-favorite");
	}
}